home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / pc / WANDR330.ZIP / SRC / EDIT.C < prev    next >
Encoding:
C/C++ Source or Header  |  1997-01-01  |  15.0 KB  |  583 lines

  1. #include "wand_head.h"
  2. #include <curses.h>
  3.  
  4. extern char *playscreen();
  5. extern void helpme();
  6. extern int inform_me();
  7.  
  8. extern int debug_disp;
  9. extern char *edit_screen;
  10. extern char screen[NOOFROWS][ROWLEN+1];
  11. extern char *edit_memory;
  12. extern char *memory_end;
  13. extern char screen_name[61];
  14. extern WINDOW *displaywin,*mapwin,*infowin,*messagewin;
  15. extern WINDOW *win;
  16.  
  17. static char *inst[] = { "    O  Boulder",
  18.             "  < >  Arrows",
  19.             "    ^  Balloon",
  20.             "    :  Earth",
  21.             "    !  Landmine",
  22.             "    *  Treasure",
  23.             "  / \\  Deflectors",
  24.             "    +  Cage",
  25.             "_ = #  Rock (# indestructable)",
  26.             "    T  Teleport",
  27.             "    A  Arrival (1 max)",
  28.             "    X  Exit (always 1)",
  29.             "    @  Start (always 1)",
  30.             "    M  Big Monster (1 max)",
  31.             "    S  Baby Monster",
  32.             "    -  Alternative space",
  33.             "    C  Time Capsule",
  34.             "    ~  Thingy",
  35.             "    B  Bomb",
  36.             NULL };
  37.  
  38. check_legality()
  39. /*
  40.    this will check: baby monster vs cage numbers,
  41.             teleport, large monster, exit and player numbers
  42.             hanging boulders/arrows etc
  43. */
  44. {
  45.     int ercount, cages, hanging, bmons, tele, arrival, you, mons, exits;
  46.     int x, y;
  47.     char buf[80];
  48.     ercount = hanging = cages = bmons = tele = arrival = you = mons = exits = 0;
  49.  
  50.     move(0,0);
  51.     addstr("Checking screen legality..."); refresh();
  52.     for (x = 0 ; x < ROWLEN ; x++)
  53.     for (y = 0 ; y < NOOFROWS ; y++) {
  54.         move(y+1,x+1); addch('?'); refresh();
  55.         switch (screen[y][x]) {
  56.         case '+': cages++; break;
  57.         case 'S': bmons++; break;
  58.         case 'T': tele++; break;
  59.         case 'A': arrival++; break;
  60.         case '@': you++; break;
  61.         case 'M': mons++; break;
  62.         case 'X': exits++; break;
  63.         case '-':
  64.         case ' ': if ((screen[y-1][x] == 'O') && (y > 0)) hanging++;
  65.             if ((screen[y+1][x] == '^') && (y < NOOFROWS)) hanging++;
  66.             if ((screen[y][x-1] == '>') && (x > 0)) hanging++;
  67.             if ((screen[y][x+1] == '<') && (x < ROWLEN)) hanging++;
  68.             break;
  69.         default : break;
  70.         }
  71.         move(y+1,x+1); addch(screen[y][x]);
  72.     }
  73.     move(20,0); addstr("                         ");
  74.     if (cages != bmons) {
  75.     ercount++;
  76.     if (cages < bmons)
  77.         sprintf(buf,"++++ Warning: %d cage(s), %d baby monster(s).",cages,bmons);
  78.     else
  79.         sprintf(buf,"**** Cage imbalance: %d cage(s), %d baby monster(s).",cages,bmons);
  80.     if (inform_me(buf,1)) return;
  81.     }
  82.     if (tele > 1) {
  83.     ercount++;
  84.     if (inform_me("++++ Warning: Too many teleports",1)) return;
  85.     }
  86.     if (arrival > ((tele > 0) ? 1 : 0)) {
  87.     ercount++;
  88.     if (tele == 0) {
  89.         if (inform_me("**** No arrivals without teleports.",1)) return;
  90.     } else if (inform_me("**** Too many arrivals.",1)) return;
  91.     }
  92.     if ((arrival == 0) && (tele > 0)) {
  93.     ercount++;
  94.     if (inform_me("**** No arrival for teleport.",1)) return;
  95.     }
  96.     if (you != 1) {
  97.     ercount++;
  98.         if (you == 0) {
  99.         if (inform_me("**** No start position.",1)) return;
  100.     } else if (inform_me("**** Too many start positions.",1)) return;
  101.     }
  102.     if (mons > 1) {
  103.     ercount++;
  104.     if (inform_me("**** Too many monsters.",1)) return;
  105.     }
  106.     if (exits != 1) {
  107.     ercount++;
  108.     if (exits == 0) {
  109.         if (inform_me("**** No exit to screen.",1)) return;
  110.     } else if (inform_me("++++ Warning: Too many exits.",1)) return;
  111.     }
  112.     if (hanging > 0) {
  113.     sprintf(buf,"++++ Warning: %d hanging boulders/arrows/balloons.",hanging);
  114.     if (inform_me(buf,1)) return;
  115.     }
  116.     ercount += hanging;
  117.     move(19,0);
  118.     if (ercount == 0)
  119.     inform_me("---- Screen OK.",0);
  120.     else {
  121.     sprintf(buf,"---- Total errors: %d",ercount);
  122.     inform_me(buf,0);
  123.     }
  124.     refresh();
  125. }
  126.  
  127. void readstring(str, size)
  128. char *str;
  129. int size;
  130. {
  131.     int count = 0;
  132.     char ch;
  133.  
  134.     for (;;) {
  135.     ch = getch();
  136.     if (ch == '\n') {
  137.         *str = '\0';
  138.         break;
  139.     }
  140.     if ((ch == '\010') || (ch == '\177')) {
  141.         if (count == 0)
  142.         continue;
  143.         str--; count--;
  144.         addstr("\b \b");
  145.         refresh();
  146.         continue;
  147.     }
  148.     if (count == size) {
  149.         printf("\007");
  150.         continue;
  151.     }
  152.     addch(ch);
  153.     *str = ch;
  154.     str++; count++;
  155.     refresh();
  156.     }
  157. }
  158.  
  159. void wreadstring(win, str, size)
  160. WINDOW *win;
  161. char *str;
  162. int size;
  163. {
  164.     int count = 0;
  165.     char ch;
  166.  
  167.     for (;;) {
  168.     ch = wgetch(win);
  169.     if (ch == '\n') {
  170.         *str = '\0';
  171.         break;
  172.     }
  173.     if ((ch == '\010') || (ch == '\177')) {
  174.         if (count == 0)
  175.         continue;
  176.         str--; count--;
  177.         waddstr(win,"\b \b");
  178.         wrefresh(win);
  179.         continue;
  180.     }
  181.     if (count == size) {
  182.         printf("\007");
  183.         continue;
  184.     }
  185.     waddch(win,ch);
  186.     *str = ch;
  187.     str++; count++;
  188.     wrefresh(win);
  189.     }
  190. }
  191.  
  192.  
  193. clearbottom()
  194. {
  195.     wmove(messagewin,0,0);
  196.     waddstr(messagewin,"                                                                            \n");
  197.     waddstr(messagewin,"                                                                            \n");
  198.     waddstr(messagewin,"                                                                            ");
  199. }
  200.  
  201.  
  202. /* Print instructions around the screen */
  203. void instruct()
  204. {
  205.     int loop;
  206.     wclear(infowin);
  207.     for (loop = 0; inst[loop]; loop++) {
  208.     wmove(infowin,loop,0);
  209.     waddstr(infowin,inst[loop]);
  210.     }
  211.     wrefresh(infowin);
  212.     wmove(messagewin,3,0);
  213.     waddstr(messagewin,"c: change name, m: change moves, q: save and exit, n/p: play game\n");
  214.     waddstr(messagewin,"Press '?' for full editor instructions. Use wanderer keys to move.");
  215.     wrefresh(messagewin);
  216. }
  217.  
  218. void noins()
  219. {
  220.     clearbottom();
  221.     wclear(infowin);
  222. }
  223.  
  224. /* save and restore screen data */
  225. screen_save(maxmoves)
  226. int maxmoves;
  227. {
  228.     char file[90];
  229.     char *oldname;
  230.     int y;
  231.  
  232.     clearbottom(); 
  233.     wmove(messagewin,0,0);
  234.     waddstr(messagewin,"Filename to write to? :");
  235.     if (edit_screen)
  236.     waddstr(messagewin,edit_screen);
  237.     else
  238.     waddstr(messagewin,"./screen");
  239.     wmove(messagewin,0,23);
  240.     wrefresh(messagewin);
  241.     waddstr(messagewin,"                      ");
  242.     wmove(messagewin,0,23);
  243.     wreadstring(messagewin,file,89);
  244.     wmove(messagewin,0,0);
  245.     addstr("                                                                "); 
  246.     wrefresh(messagewin);
  247.     oldname = edit_screen;
  248.     if (file[0]) edit_screen = file;
  249.     for (y = 0; y <= NOOFROWS; y++)    /* make sure screen written */
  250.     if (screen[y][ROWLEN-1] == ' ')    /* correctly... */
  251.         screen[y][ROWLEN-1] = '-';
  252.     wscreen(0,maxmoves);
  253.     for (y = 0; y <= NOOFROWS; y++)
  254.     if (screen[y][ROWLEN-1] == '-')
  255.         screen[y][ROWLEN-1] = ' ';
  256.     edit_screen = oldname;
  257. }
  258.  
  259. screen_read(maxmoves)
  260. int *maxmoves;
  261. {
  262.     static char file[90];
  263.     int y;
  264.  
  265.     clearbottom();
  266.     wmove(messagewin,0,0);
  267.     waddstr(messagewin,"Filename to read from? :");
  268.     wmove(messagewin,0,24);
  269.     wrefresh(messagewin);
  270.     wreadstring(messagewin,file,89);
  271.     wmove(messagewin,0,0);
  272.     waddstr(messagewin,"                                                   ");
  273.     wrefresh(messagewin);
  274.     if (!file[0]) return;
  275.     edit_screen = file;
  276.     rscreen(0,maxmoves);
  277.     for (y = 0; y <= NOOFROWS; y++)
  278.     if (screen[y][ROWLEN-1] == '-')
  279.     screen[y][ROWLEN-1] = ' ';
  280. }
  281.  
  282. /* save and restore edit memory data */
  283. edit_save()
  284. {
  285.     char file[90];
  286.     int i = 0, fd;
  287.  
  288.     clearbottom(); 
  289.     wclear(messagewin);
  290.     wmove(messagewin,0,0);
  291.     waddstr(messagewin,"Solution file to save? :");
  292.     wrefresh(messagewin);
  293.     wreadstring(messagewin,file,89);
  294.     wmove(messagewin,0,0); 
  295.     waddstr(messagewin,"                                                    "); 
  296.     wrefresh(messagewin);
  297. #ifdef TOS
  298.     if ((fd = open(file,O_WRONLY|O_CREAT|O_TRUNC,0644)) < 0)
  299. #else
  300.     if ((fd = open(file,O_WRONLY|O_CREAT|O_TRUNC,0644)) == -1)
  301. #endif
  302.     inform_me("File cannot be opened for writing.",0);
  303.     else {
  304.     i = write(fd, edit_memory, (int)(memory_end - edit_memory));
  305.     if (i < 0) inform_me("Write error on file.",0);
  306.     }
  307. }
  308.  
  309. edit_restore()
  310. {
  311.     char file[90];
  312.     int i = 0, fd;
  313.  
  314.     wclear(messagewin);
  315.     wmove(messagewin,0,0);
  316.     waddstr(messagewin,"Solution file to load? :"); 
  317.     wrefresh(messagewin);
  318.     wreadstring(messagewin,file,89);
  319.     wmove(messagewin,0,0);
  320.     waddstr(messagewin,"                                                 ");
  321.     wrefresh(messagewin);
  322.     wmove(messagewin,0,0);
  323. #ifdef TOS
  324.     if ((fd = open(file,O_RDONLY)) < 0)
  325. #else
  326.     if ((fd = open(file,O_RDONLY)) == -1)
  327. #endif
  328.     inform_me("File cannot be opened for reading.",0);
  329.     else {
  330.     i = read(fd, edit_memory, EMSIZE);
  331.     if (i < 0) inform_me("Read error on file.",0);
  332.     if (i == 0) inform_me("File empty.",0);
  333.     if (i > 0) {
  334.         sprintf(file,"Read in %d moves.",i);
  335.         inform_me(file,0);
  336.         memory_end = edit_memory + i;
  337.     }
  338.     }
  339. }
  340.  
  341. /* Actual edit function */
  342.  
  343. void editscreen(num, score, bell, maxmoves, keys)
  344. int  num, maxmoves,
  345.      *bell;
  346. long *score;
  347. char keys[10];
  348. {
  349.     int  mmbkup, x, y, sx = 0, sy = 0, quit = 0, nx, ny, nosave = 0;
  350.     char (*frow)[ROWLEN+1] = screen;
  351.     int ch; /* NEW SS 22-12-98 */
  352.     char buffer[50];
  353.     char *howdead;
  354.     char *storage;
  355.  
  356.     if ((storage = (char *) malloc(sizeof(screen))) == NULL) {
  357.     addstr("Oops... can't malloc a backup screen!\n\n");
  358.     return;
  359.     }
  360.  
  361.     for (x = 0; x <= ROWLEN; x++)
  362.     for (y = 0; y < NOOFROWS; y++) {
  363.         if (screen[y][x] == '@') {
  364.         sx = x;
  365.         sy = y;
  366.         }
  367.         if (screen[y][x] == '-')
  368.         screen[y][x] = ' ';
  369.     }
  370.     x = sx;
  371.     y = sy;
  372.     if (maxmoves != 0)
  373.     sprintf(buffer,"Moves   : %d        ",maxmoves);
  374.     else
  375.     strcpy(buffer,"Moves   : Unlimited");
  376.     debug_disp = 1;
  377.     win = mapwin;
  378.     wbkgd(win,COLOR_PAIR(1));
  379.     map(frow);
  380.     wmove(messagewin,0,0);
  381.     waddstr(messagewin,buffer);
  382.     wmove(messagewin,1,0);
  383.     waddstr(messagewin,"                                                 ");
  384.     wmove(messagewin,1,0);
  385.     waddstr(messagewin,"Name    : ");
  386.     waddstr(messagewin,screen_name);
  387.     wrefresh(messagewin);
  388.  
  389. /* ACTUAL EDIT FUNCTION */
  390.  
  391.     instruct();
  392.     while (!quit) {
  393.     wmove(win,y+1,x+1);
  394.     wrefresh(win);
  395.     ch = wgetch(win);
  396.  
  397.     nx = x;
  398.     ny = y;
  399.  
  400.     if (ch == keys[3] || ch == keys[2] || ch == keys[1] || ch == keys[0])
  401.         {
  402.         if (ch == keys[3]) nx++;
  403.         if (ch == keys[2]) nx--;
  404.         if (ch == keys[1]) ny++;
  405.         if (ch == keys[0]) ny--;
  406.     }
  407.         else if (ch == KEY_RIGHT || ch == KEY_LEFT
  408.               || ch == KEY_UP || ch == KEY_DOWN)
  409.         {
  410.             if (ch == KEY_RIGHT) nx++;
  411.             if (ch == KEY_LEFT)  nx--;
  412.             if (ch == KEY_DOWN)  ny++;
  413.             if (ch == KEY_UP)    ny--;
  414.         }
  415.     else if (ch == 'q') {
  416.         clearbottom();
  417.         wmove(messagewin,0,0);
  418.         waddstr(messagewin,"Do you want to save the sreen?");
  419.         wrefresh(messagewin);
  420.         ch = wgetch(messagewin);
  421.         if(ch == 'n')
  422.           nosave=1;
  423.         break;
  424.     } else if (ch == 'x') {
  425.         clearbottom();
  426.         wmove(messagewin,0,0);
  427.         waddstr(messagewin,"You will lose any changes made\n this session - are you sure? (y/n)");
  428.         wrefresh(messagewin);
  429.         ch = wgetch(messagewin);
  430.         if (ch != 'y') {
  431.         noins();
  432.         instruct();
  433.         wrefresh(messagewin);
  434.         } else {
  435.         nosave = 1;
  436.         waddstr(messagewin,"\n");
  437.         wrefresh(messagewin);
  438.         break;
  439.         }
  440.     } else if (ch == 'm') {    /* change to number of moves for the screen */
  441.         clearbottom();
  442.         wmove(messagewin,0,0);
  443.         waddstr(messagewin,"How many moves for this screen? :");
  444.         wrefresh(messagewin); echo();
  445.         scanf("%d",&maxmoves);noecho();
  446.         if (maxmoves < 0) maxmoves = 0;
  447.         if (maxmoves != 0)
  448.         sprintf(buffer,"Moves   : %d        ",maxmoves);
  449.         else
  450.         strcpy(buffer,"Moves   : Unlimited ");
  451.         instruct();
  452.         waddstr(messagewin,buffer);
  453.         wrefresh(messagewin);/* for some reason, this seems to add a '.'
  454.                     /* to the map... I've no idea why yet... */
  455.     } else if (ch == 'c') {    /* change name */
  456.         clearbottom();
  457.         wmove(messagewin,0,0);
  458.         waddstr(messagewin,"New name: ");
  459.         wrefresh(messagewin);
  460.         wreadstring(messagewin,screen_name,60);
  461.         instruct();
  462.         wmove(messagewin,0,0);
  463.         waddstr(messagewin,"                                       ");
  464.         wmove(messagewin,0,0);
  465.         waddstr(messagewin,"Name  : ");
  466.         waddstr(messagewin,screen_name);
  467.         wrefresh(messagewin);
  468.     } else if (ch == 'p' || ch == 'n') {    /* play the game (test) */
  469.         noins();
  470.         mmbkup = maxmoves;
  471.         bcopy(screen,storage,sizeof(screen));
  472.         if (ch == 'p') {
  473.         debug_disp = 0;
  474.         wclear(win);
  475.         }
  476.         *score = 0;
  477.             wbkgd(win,COLOR_PAIR(2));
  478.         howdead = playscreen(&num,score,bell,maxmoves,keys);
  479.         wmove(messagewin,0,0);
  480.         if (howdead != 0)
  481.         waddstr(messagewin,howdead);
  482.         else
  483.         waddstr(messagewin,"DONE!");
  484.         wprintw(messagewin,"; hit any key to continue\n");
  485.         wrefresh(messagewin);
  486.         ch = wgetch(messagewin);
  487.         wclear(mapwin);
  488.         bcopy(storage,screen,sizeof(screen));
  489.         maxmoves = mmbkup;
  490.         debug_disp = 1;
  491.             wbkgd(win,COLOR_PAIR(1)); 
  492.         map(frow);
  493.         if (maxmoves != 0)
  494.         sprintf(buffer,"Moves   : %d        \n",maxmoves);
  495.         else
  496.         strcpy(buffer,"Moves   : Unlimited\n");
  497.         wmove(messagewin,0,0);
  498.         waddstr(messagewin,buffer);
  499.         waddstr(messagewin,"Name    : ");
  500.         waddstr(messagewin,screen_name);
  501.             wrefresh(messagewin);
  502.         instruct();
  503.     } else if (ch == 18) {    /* ctrl r -- read memory data */
  504.         {edit_restore(); instruct();}
  505.     } else if (ch == 23) {    /* ctrl w -- write memory data */
  506.         {edit_save(); instruct();}
  507.     } else if (ch == 7) {    /* ctrl g -- read screen */
  508.         screen_read(&maxmoves);
  509.         map(frow);
  510.         instruct();
  511.     } else if (ch == 16) {    /* ctrl p -- write screen */
  512.         screen_save(maxmoves);
  513.         instruct();
  514.     } else if (ch == 12) {    /* ctrl l -- redraw screen  */
  515.         clear();
  516.         map(frow);
  517.         if (maxmoves != 0)
  518.         sprintf(buffer,"Moves   : %d        \n",maxmoves);
  519.         else
  520.         strcpy(buffer,"Moves   : Unlimited\n");
  521.         move(18,0);
  522.         addstr(buffer);
  523.         addstr("Name    : ");
  524.         addstr(screen_name);
  525.         instruct();
  526.     } else if (ch == 'L') {
  527.         clearbottom();
  528.         check_legality();
  529.         instruct();
  530.     } else if (ch == '?') {
  531.         helpme(0);
  532.         if (maxmoves != 0)
  533.         sprintf(buffer,"Moves   : %d        \n",maxmoves);
  534.         else
  535.         strcpy(buffer,"Moves   : Unlimited\n");
  536.         map(frow);
  537.     } else if ((ch == 127) || (ch == 8)) {  /* delete key */
  538.         if (--nx < 0) {
  539.         nx = ROWLEN -1;
  540.         if (--ny < 0) ny = NOOFROWS -1;
  541.         }
  542.         screen[ny][nx] = ' ';
  543.         move(ny+1,nx+1);
  544.         addch(' ');
  545.     } else {
  546.         if (ch >= 'a' && ch <= 'z') ch = ch - 'a' + 'A';
  547.         if (ch == '"') ch = getchar();
  548.         if (!(ch < ' ')) {
  549.         screen[y][x] = ch;
  550.         wmove(win,y+1,x+1);
  551.         waddch(win,ch);
  552.         nx++;
  553.         }
  554.     }
  555.     if (nx < 0) {
  556.         nx = ROWLEN-1;
  557.         ny--;
  558.     }
  559.     if (nx >= ROWLEN) {
  560.         nx = 0;
  561.         ny++;
  562.     }
  563.     if (ny < 0) ny = NOOFROWS-1;
  564.     if (ny >= NOOFROWS) ny = 0;
  565.     move(ny+1,nx+1);
  566.     x = nx;
  567.     y = ny;
  568.     }
  569.  
  570.     noins();
  571.     move(20,0);
  572.     refresh();
  573.  
  574.     if (!nosave) {
  575.     for (y = 0; y <= NOOFROWS; y++) /* certain editors - eg ded - have a */
  576.                 /* habit of truncating trailing spaces */
  577.                 /* so this should stop them! */
  578.         if (screen[y][ROWLEN-1] == ' ')
  579.         screen[y][ROWLEN-1] = '-';
  580.     wscreen(num,maxmoves);
  581.     }
  582. }
  583.